package com.suning.app.snpaasadmin.api;

import com.suning.app.snpaasadmin.action.LoginAction;
import com.suning.app.snpaasadmin.exception.BoshException;
import com.suning.app.snpaasadmin.pojo.CfVersion;
import com.suning.app.snpaasadmin.util.Constants;
import com.suning.app.snpaasadmin.util.HttpClientResponse;
import com.suning.app.snpaasadmin.util.HttpClientUtil;
import com.suning.app.snpaasadmin.util.HttpStatusUtil;
import com.suning.app.snpaasadmin.util.JsonUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.http.HttpHost;
import org.apache.http.protocol.HttpContext;


public class CfVersionAPI {
	
	public static List<CfVersion> getCfVersion(HttpSession session, Integer type) throws Exception {
		HttpHost httpHost = (HttpHost) session.getAttribute(LoginAction.CFAUTHHOST);
		HttpContext httpContext = (HttpContext) session.getAttribute(LoginAction.CFBASICAUTH);

		String path = httpHost.toString() + "/releases";
		HttpClientResponse response = HttpClientUtil.doGet(path, null, null, httpHost, httpContext);
		ArrayList<CfVersion> cfVersionList = new ArrayList<CfVersion>();

		if (!HttpStatusUtil.isSuccessStatus(response.getStatus())) {
		    String content = response.getBody();
            Map<String, Object> data = JsonUtil.convertJsonToMap(content);
            throw new BoshException(String.valueOf(response.getStatus()), data.get("description").toString());
		}
		String body = response.getBody();
        List<Map<String, Object>> bodyList = JsonUtil.convertJsonToList(body);
        
        for (int i = 0; i < bodyList.size(); i++) {
            CfVersion cv = new CfVersion();
            Map<String, Object> bodyMap = bodyList.get(i);
            String name = bodyMap.get("name").toString();
            
            //根据type匹配出不同的结果  type 1:cf   2:mysql
            if(type == Constants.TYPE_CF_VERSION && Constants.CF_VERSION_NAME.equals(name)) {
                cv.setName(name);
                List<Map<String, Object>> verListMap = JsonUtil.convertJsonToList(JsonUtil.convertToJson(bodyMap.get("release_versions")));
                
                for(int j = 0; j<verListMap.size(); j++) {
                    String version = verListMap.get(j).get("version").toString();
                    cv.setVersion(version);
                }
                cfVersionList.add(cv);
            }
            if(type == Constants.TYPE_MYSQL_VERSION && Constants.MYSQL_VERSION_NAME.equals(name)) {
                cv.setName(name);
                List<Map<String, Object>> verListMap = JsonUtil.convertJsonToList(JsonUtil.convertToJson(bodyMap.get("release_versions")));
                
                for(int j = 0; j<verListMap.size(); j++) {
                    String version = verListMap.get(j).get("version").toString();
                    cv.setVersion(version);
                }
                cfVersionList.add(cv);
            }
        }
		return cfVersionList;
	}
	
	public static String uploadCfVesionApi(HttpSession session, File file) throws Exception {
	    HttpHost httpHost = (HttpHost) session.getAttribute(LoginAction.CFAUTHHOST);
        HttpContext httpContext = (HttpContext) session.getAttribute(LoginAction.CFBASICAUTH);

        String urlPath = httpHost.toString() + "/releases";
        HttpClientResponse response = HttpClientUtil.doPostWithFile(urlPath, file, httpHost, httpContext);
        String body = "";
        if (!HttpStatusUtil.isSuccessStatus(response.getStatus())) {
            String content = response.getBody();
            Map<String, Object> data = JsonUtil.convertJsonToMap(content);
            throw new BoshException(String.valueOf(response.getStatus()), data.get("description").toString());
        }
        body = response.getBody();
        return body;
	}

}
